package com.cos.hospital.ui.login;

import android.content.Intent;
import android.text.Editable;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.cos.hospital.R;
import com.cos.hospital.code.Code;
import com.cos.hospital.code.helper.KTextWatcher;
import com.cos.hospital.init.BaseActivity;
import com.cos.hospital.tool.URegular;
import com.cos.hospital.tool.USP;
import com.cos.hospital.tool.UTxt;
import com.cos.hospital.tool.glide.UGlide;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Description: 登录 类别
 * @Author: Kosmos
 * @Date: 2018年9月9日 17:01
 * @Email: KosmoSakura@foxmail.com
 */
public class LoginActivity extends BaseActivity {
    @BindView(R.id.login_bg)
    ImageView iBg;
    @BindView(R.id.login_eye)
    ImageView iEye;
    @BindView(R.id.edt_phone)
    EditText edtPhone;
    @BindView(R.id.edt_password)
    EditText edtPassword;
    @BindView(R.id.login_done)
    TextView tDone;
    private String canLogin;
    private ULogin login;

    @Override
    protected int layout() {
        return R.layout.activity_login;
    }

    @Override
    protected void init() {
        UGlide.loadImage(this, R.mipmap.bg_login, iBg);
        edtPhone.setText(USP.instance().getString(Code.SP.USER_NAME));
//        edtPassword.setText(USP.instance().getString(Code.SP.USER_PWD));
//        edtPhone.setText("13980594826");
//        edtPassword.setText("123456");
        iEye.setSelected(false);
        edtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
        checkLoginbtn();
        edtPhone.setSelection(edtPhone.getText().length());//光标末尾
        edtPassword.setSelection(edtPassword.getText().length());//光标末尾
    }

    @Override
    protected void logic() {
        edtPhone.addTextChangedListener(new KTextWatcher() {
            @Override
            public void afterTextChanged(Editable txtEnd) {
                if (txtEnd.length() > 10 && !URegular.checkPhoneNum(txtEnd.toString())) {
                    showNotice(getString(R.string.phone_number_error));
                }
                checkLoginbtn();
            }
        });
        edtPassword.addTextChangedListener(new KTextWatcher() {
            @Override
            public void afterTextChanged(Editable txtEnd) {
                if (txtEnd.length() > 16) {
                    showNotice(getString(R.string.password_format_error));
                }
                checkLoginbtn();
            }
        });
    }

    private void checkLoginbtn() {
        canLogin = checkState();
        if (UTxt.isEmpty(canLogin)) {
            tDone.setBackgroundResource(R.drawable.rect_rim);
        } else {
            tDone.setBackgroundResource(R.drawable.rect_gray_light);
        }
    }

    private String checkState() {
        if (UTxt.isEmpty(edtPhone.getText().toString())) {
            return getString(R.string.phone_number_enter);
        }
        if (!URegular.checkPhoneNum(edtPhone.getText().toString())) {
            return getString(R.string.phone_number_error);
        }
        if (UTxt.isEmpty(edtPassword)) {
            return getString(R.string.password_enter);
        }
        if (edtPassword.length() < 6 || edtPassword.length() > 16) {
            return getString(R.string.password_format_error);
        }
        return "";
    }

    @OnClick({R.id.login_done, R.id.login_forget_password, R.id.login_eye})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.login_done:
                if (UTxt.isEmpty(canLogin)) {
                    login = new ULogin(this, getServes(),
                        edtPhone.getText().toString(), edtPassword.getText().toString());
                    login.runLogin();
                } else {
                    showNotice(canLogin);
                }
                break;
            case R.id.login_forget_password:
                startActivity(new Intent(this, PasswordResetAcitivity.class));
                break;
            case R.id.login_eye:
                if (iEye.isSelected()) {
                    iEye.setSelected(false);
                    edtPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                } else {
                    iEye.setSelected(true);
                    edtPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                }
                break;
        }
    }

    @Override
    protected void onDestroy() {
        if (login != null) {
            login.onDestroy();
        }
        super.onDestroy();
    }
}
